package com.xxxx.server.controller;

import com.xxxx.server.pojo.Notice;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.RespPageBean;
import com.xxxx.server.service.INoticeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.xml.ws.Response;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/notice")
public class NoticeController公告 {

    @Autowired
    private INoticeService iNoticeService;

    /**
     *查询所有的列表信息
     * @return
     */
   @ApiOperation(value = "获取所有公告信息")
    @GetMapping("/list")
    public RespPageBean listPage(@RequestParam(defaultValue = "1" ) Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 Notice notice) throws Exception {
        return iNoticeService.findList(currentPage,size,notice);

    }



    /**
     * 根据id删除一条消息
     */
    @ApiOperation(value = "根据id删除消息")
    @DeleteMapping("/remove/{id}")
    public RespBean remove(@PathVariable() String id){
        if (iNoticeService.removeById(id)){
            return RespBean.success("删除消息成功");
        }
        return RespBean.error("删除失败");
    }

    /**
     * 更改一条消息
     */
    @ApiOperation(value = "更改消息")
    @PutMapping("/update")
    public RespBean update(@RequestBody Notice notice){
        if (iNoticeService.updateById(notice)) {
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败了");
    }


    /**
     * 增加列表信息
     */
    @ApiOperation(value = "增加一条公告信息")
    @PostMapping("/save")
    public RespBean save(@RequestBody Notice notice){
        Date time = new Date(System.currentTimeMillis());
        notice.setCreate_time(time);
        System.out.println(time);
        notice.setId(UUID.randomUUID().toString());
        System.out.println(notice.getId());

        notice.setTitle("is me title");
        notice.setContent("is me content");
        notice.setUpdate_time(new Date());
        notice.setCreate_by("2");
        notice.setType(1);
        if (iNoticeService.save(notice)){
            return  RespBean.success("更新成功了");
        }
        return RespBean.error("更新失败了");
    }
}
